<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends CommonController
{

    public $limit = 15;

    public function index()
    {
        $data = M('yh_tag')->select();
        $map = $this->_where();
        $tag_id = '0';
        if(isset($_GET['tag'])){
            $tag_id = trim(I('get.tag'));
        }
        $count = M('yh_user')->where($map)->count();// 查询满足要求的总记录数
        $Page = new \Org\Bjy\Page($count, $this->limit);
//        $Page       = new \Think\Page($count,$this->limit);

        $list = M('yh_user')
            ->Field('yh_user.name as aname,yh_tag.name as bname,yh_user.*')
            ->join('left join yh_tag on yh_user.tag_id = yh_tag.tag_id')
            ->where($map)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();

        $show = $Page->show();
        $this->assign('tag_id',$tag_id);
        $this->assign('date', $list);// 赋值数据集
        $this->assign('page', $show);
        $this->assign('data', $data);// 赋值分页输出
        $this->display();

    }

    public function _where()
    {

        if (count($_GET)) {

            $tag_id = trim(I('get.tag'));
            $name = trim(I('get.username'));
            $tel = trim(I('get.tel'));
            //标准时间转换

            $arr = array('1=1');
            if (!empty($tag_id) && $tag_id == 0) {
                $arr[] = "yh_user.tag_id = 1 or yh_user.tag_id = 2 or yh_user.tag_id =3";
            }
            if (!empty($tag_id) && $tag_id != 0) {
                $arr[] = "yh_user.tag_id = '$tag_id'";
            }
            if (!empty($name)) {
                $arr[] = "yh_user.name = '$name'";
            }
            if (!empty($tel)) {
                $arr[] = "yh_user.phone = '$tel'";
            }

            $where = implode(' and ', $arr);
            return $where;
        }


    }

    public function detail()
    {
        $id = I('get.id');
        $_SESSION['uid'] = $id;
        //yh_order yh_user
        $model = M('yh_user');
        $count = $model->where('yh_user.id = ' . $id)->join('yh_order on yh_user.id = yh_order.uid')->count();// 查询满足要求的总记录数

        $Page = new \Org\Bjy\Page($count, $this->limit);
//        $Page       = new \Think\Page($count,$this->limit);// 实例化分页类 传入总记录数和每页显示的记录数

        $data = M('yh_user')
//            ->Field('yh_user as yuser,yh_order as yorder')
            ->where('yh_user.id = ' . $id)
            ->join('left join yh_order on yh_user.id = yh_order.uid')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $show = $Page->show();
        $this->assign('data', $data);// 赋值数据集
        $this->assign('page', $show);
        $this->display();
    }


}